package controler;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import view.ListView;
import ema.tpjee.banking.dao.SessionFacade;
import ema.tpjee.banking.model.Customer;



public class CustomerControler  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6485014212164711258L;
	//private BankControler bankControler;
	//private Customer currentCustomer;
	private ListView view = new ListView();
	private int id=100;
	private SessionFacade sessionFacade;

	
	public CustomerControler() {
		super();
		System.out.println("creating CustomerControler");
	}

	public SessionFacade getSessionFacade() {
		return sessionFacade;
	}

	public void setSessionFacade(SessionFacade sessionFacade) {
		this.sessionFacade = sessionFacade;
		System.out.println(this.getClass().getName()+" setSessionFacade");

	}

	public ListView getView() {
		return view;
	}

	private Customer getCustomerById(String id) {
		int customerid = Integer.parseInt(id);
		for (Iterator<Customer> i = getList().iterator(); i.hasNext();) {
			Customer cust = i.next();
			if (cust.getId() == customerid)
				return cust;
		}
		return null;
	}

	public List<Customer> getList() {
		return sessionFacade.getCustomers();
		
	}
/*
	public Customer getCurrentCustomer() {
		return currentCustomer;
	}

	public void setCurrentCustomer(Customer currentCustomer) {
		this.currentCustomer = currentCustomer;
	}
*/
	private void createAdd() {
		Customer c = new Customer(id++, "nomane", "noformane", "noaddress",
				"nozip");
		getList().add(c);
		sessionFacade.setCurrentCustomer(c);
	}

	private void dodelete(Customer customer) {
		for (Iterator i = getList().iterator(); i.hasNext();) {
			Customer cust = (Customer) i.next();
			if (customer == cust)
				i.remove();
		}
	}

	public void add(Customer customer) {
		getList().add(customer);
	}

	public String apply() {
		System.out.println("apply");
		return "edit_customer";
	}

	public String cancel() {
		System.out.println("cancel");
		return "list_customer";
	}

	public String create() {
		System.out.println("create");
		//currentCustomer = null;
		createAdd();
		return "edit_customer";
	}

	public String save() {
		System.out.println("save");
		// saveCustomer(currentCustomer);
		return "list_customer";
	}

	public String next() {
		System.out.println("next");
		sessionFacade.setCurrentCustomer(nextCustomer(sessionFacade.getCurrentCustomer()));
		return "edit_customer";
	}
	
	private Customer nextCustomer(Customer c) {
		for (Iterator<Customer> i = getList().iterator(); i.hasNext();) {
			Customer cust = i.next();
			if (cust.getId() == c.getId())
			try {
				return i.next();
			} catch (Exception e) {
				return null;
			}			
		}
		return null;
	}

	public String prior() {
		System.out.println("prior");
		return "list_customer";
	}
	public String copy() {
		System.out.println("copy");
		return "list_customer";
	}
	
	public String foobar() {
		System.out.println("foobar");
		return "list_customer";
	}
	
	
	public String delete() {
		System.out.println("delete");
	    Customer prior = sessionFacade.getCustomerDao().prior(sessionFacade.getCurrentCustomer());
		dodelete(sessionFacade.getCurrentCustomer());
		sessionFacade.setCurrentCustomer(prior);
		return "list_customer";
	}

	public String edit() {
		System.out.println("edit");
		String customerId = view.getIdCmdValue();
		System.out.println(customerId);
		sessionFacade.setCurrentCustomer(getCustomerById(customerId));
		return "edit_customer";
	}



	public void setView(ListView view) {
		//this.view = view;
	}


}
